package com.zb.mvc.controller.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.dao.ext.base.OmsCodeRepo;
import com.zb.entity.base.JsonForList;
import com.zb.entity.base.OmsCodeEntity;
import com.zb.entity.charger.OmsChargerRateEntity;
import com.zb.util.OmsCodesConstants;
import com.zb.util.OmsConstant;
import com.zb.util.SelectItem;
import com.zb.util.SelectItemImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/code")
public class CodeController extends BaseController
{
	@Autowired
	OmsCodeRepo omsCodeRepo;

	@RequestMapping(value = "retrieve")
	@ResponseBody
	public JsonForList<SelectItemImpl> retrieve(String codeType)
	{
		Class[] clz =  OmsCodesConstants.class.getDeclaredClasses();
		for(Class c : clz)
		{
			if(c.getSimpleName().equals(codeType) && c.isEnum())
			{
				Class[] list_interfaces =  c.getInterfaces();
				for(Class i : list_interfaces)
				{
					if(i.equals(SelectItem.class))
					{
						SelectItem[] items = (SelectItem[])c.getEnumConstants();
						return new JsonForList<>(SelectItemImpl.convert(items), OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
					}
				}
			}
		}

		List<OmsCodeEntity> list = omsCodeRepo.findByCodeTypeAndValidFlag(codeType, 1);
		return new JsonForList<>(SelectItemImpl.convert(list), OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
}